package com.kuaimacode.kframework.admin.controller.category;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.category.AgentServService;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminUser;
import com.kuaimacode.kframework.mybatis.models.category.AgentServ;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.service.DictService;
import com.kuaimacode.kframework.util.DictUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Administrator
 * @date 2019/03/22
 */

/**
 * Controller
 */
@Controller
@RequestMapping({"/service"})
public class ServiceController {
	private static String PAGE_FILE_NAME = "service/";

	@Autowired
	private AgentServService agentServiceServices;
	@Autowired
	private DictService dictService;


	@RequestMapping(value = {"/oneclass/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<Dict> pageInfo = dictService.getChildrenPage(pageNum,pageSize,"SERVICE_CLASS");
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/oneclass/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		Dict dict = new Dict();
		map.put("service", dict);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/oneclass/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		Dict dict = dictService.getById(id);
		map.put("service", dict);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}


	@RequestMapping(value = {"/oneclass/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(Dict dict, Boolean isAdd,ModelMap map) {
		try {
			Subject subject = SecurityUtils.getSubject();
			SysAdminUser principal = (SysAdminUser)subject.getPrincipal();
			if(isAdd){
				// 新建保存
				dict.setLevel(Constant.DictLevelEnum.SECOND.getCode());
				dict.setTitle("服务大类");
				dict.setName(Constant.SERVICE_CLASS_DICT_NAME);
				dict.setType((byte)2);
				dict.setParentId(30L);
				//查找最大code
				dict.setCode((dictService.getMaxCode(Constant.SERVICE_CLASS_DICT_NAME)+1)+"");
				dict.setStatus((byte)1);
				dict.setCreateAt(new Date());
				dict.setCreateBy(principal.getUserId());

				dictService.save(dict);


			}else{
				// 编辑保存
				dictService.updateById(dict);

				UpdateWrapper updateWrapper = new UpdateWrapper();
				updateWrapper.set("title",dict.getValue());
				updateWrapper.eq("name",Constant.SERVICE_CLASS_DICT_NAME+"_"+dict.getCode());
				dictService.update(updateWrapper);
			}
			//清理缓存

			DictUtil.clearCache(Constant.SERVICE_CLASS_DICT_NAME);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/oneclass/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			Dict dict = dictService.getById(id);
			dict.setStatus((byte)2);
			dictService.updateById(dict);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}
	@RequestMapping(value = "/oneclass/active/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult active(@PathVariable String id, ModelMap map) {
		try {
			Dict dict = dictService.getById(id);
			dict.setStatus((byte)1);
			dictService.updateById(dict);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}
}
